package cn.dt70.service.impl;

import cn.dt70.dao.aqbProject.AqbProjectMapper;
import cn.dt70.pojo.AqbProject;
import cn.dt70.service.AqbProjectService;
import cn.dt70.utils.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AqbProjectServiceImpl implements AqbProjectService {

    private static final Logger LOGGER = Logger.getLogger(AqbProjectServiceImpl.class);

    @Resource
    private AqbProjectMapper aqbProjectMapper;

    @Override
    public boolean addProBase(AqbProject aqbProject) {
        boolean flag = false;

        try {
            int row = aqbProjectMapper.saveProBase(aqbProject);
            if (row == 1) {
                flag = true;
            }
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.addProBase -->" + e.getMessage());
        }

        return flag;
    }

    @Override
    public boolean modifyProject(AqbProject aqbProject) {
        boolean flag = false;

        try {
            int row = aqbProjectMapper.updateProject(aqbProject);
            if (row == 1) {
                flag = true;
            }
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.modifyProject -->" + e.getMessage());
        }

        return flag;
    }

    @Override
    public List<AqbProject> findReturnProjectList(Integer createrId, Integer proStatus1, Integer proStatus2, PageSupport pageSupport) {

        return findProjectList(createrId, null, proStatus1, proStatus2, pageSupport);
    }

    @Override
    public List<AqbProject> findAssessNeedProjectList(PageSupport pageSupport) {
        return findProjectList(null, null, 0, null, pageSupport);
    }

    @Override
    public List<AqbProject> findAssessdProjectList(Integer proType,PageSupport pageSupport) {
        return findProjectList(null, proType, 1, null, pageSupport);
    }

    /*@Override
    public List<AqbProject> findProjectList(Integer createrId,Integer proType,Integer proStatus1,Integer proStatus2, Integer pageIndex) {
        List<AqbProject> aqbProjects = new ArrayList<>();
        Integer currentPageNo = 1;
        if (null!=pageIndex && !pageIndex.equals("")){
            currentPageNo=pageIndex;
        }
        PageSupport pageSupport = new PageSupport();

        try {
            Integer totalLogs = aqbProjectMapper.selectCountOfProject(createrId,proType,proStatus1,proStatus2);
            pageSupport.setTotalCount(totalLogs);
            pageSupport.setCurrentPageNo(currentPageNo);
            aqbProjects= aqbProjectMapper.selectAllProjects(createrId,proType,proStatus1,proStatus2,(pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize(),pageSupport.getPageSize());
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.findProjectList -->"+e.getMessage());
        }

        return aqbProjects;
    }*/

    @Override
    public List<AqbProject> findProjectList(Integer createrId, Integer proType, Integer proStatus1, Integer proStatus2, PageSupport pageSupport) {
        List<AqbProject> aqbProjects = new ArrayList<>();

        try {
//            Integer totalLogs = findTotalCountOfProjectList(createrId,proType,proStatus1,proStatus2);
//            pageSupport.setTotalCount(totalLogs);
            if (null != pageSupport) {
                aqbProjects = aqbProjectMapper.selectAllProjects(createrId, proType, proStatus1, proStatus2, (pageSupport.getCurrentPageNo() - 1) * pageSupport.getPageSize(), pageSupport.getPageSize());
            }else{
                aqbProjects = aqbProjectMapper.selectAllProjects(createrId,proType,proStatus1,proStatus2,-1,0);
            }

        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.findProjectList --> " + e.getMessage());
        }

        return aqbProjects;
    }

    @Override
    public Integer findTotalCountOfProjectList(Integer createrId, Integer proType, Integer proStatus1, Integer proStatus2) {
        Integer count = 0;

        try {
            count = aqbProjectMapper.selectCountOfProject(createrId, proType, proStatus1, proStatus2);
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.findTotalCountOfProjectList --> " + e.getMessage());
        }

        return count;
    }

    @Override
    public AqbProject findOneProject(Integer proId, String proCode) {
        AqbProject aqbProject = new AqbProject();

        try {
            aqbProject = aqbProjectMapper.selectOneProject(proId, proCode);
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.findOneProject -->" + e.getMessage());
        }

        return aqbProject;
    }

    @Override
    public AqbProject findSpecialProject(Integer proType) {
        AqbProject aqbProject = new AqbProject();

        try {
            aqbProject = aqbProjectMapper.selectSpecialProject(proType);
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.findSpecialProject -->" + e.getMessage());
        }

        return aqbProject;
    }

    @Override
    public boolean modifyDetailIdOfProject(Integer detailId, Integer proId) {
        AqbProject tempProject = findOneProject(proId, null);
        tempProject.setProductId(detailId);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyCurrentBids(Integer newBid, Integer proId) {
        AqbProject tempProject = findOneProject(proId, null);
        Integer oldCurrent = tempProject.getCurrentAmount();
        Integer insertCurrent = oldCurrent + newBid;
        tempProject.setCurrentAmount(insertCurrent);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyAsseseDate(Date date, Integer proId) {
        AqbProject tempProject = findOneProject(proId, null);
        tempProject.setAssessDate(date);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyFullDate(Date date, Integer proId) {
        AqbProject tempProject = findOneProject(proId, null);
        tempProject.setFullDate(date);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyStatus(Integer status, Integer proId) {
        AqbProject tempProject = findOneProject(proId, null);
        tempProject.setProStatus(status);

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyPayMoneySum(BigDecimal newPayMoney, Integer proId) {
        AqbProject tempProject = findOneProject(proId, null);
        tempProject.setPayMoneySum(tempProject.getPayMoneySum().add(newPayMoney));

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyPayInterestSum(BigDecimal newPayInterest, Integer proId) {
        AqbProject tempProject = findOneProject(proId, null);
        tempProject.setPayInterestSum(tempProject.getPayInterestSum().add(newPayInterest));

        return modifyProject(tempProject);
    }

    @Override
    public boolean modifyPayableInterest(BigDecimal payableInterest, Integer proId) {
        AqbProject tempProject = findOneProject(proId, null);
        tempProject.setPayableInterest(payableInterest);

        return modifyProject(tempProject);
    }

    @Override
    public List<AqbProject> proList(Integer createrId, Integer proType, Integer status1,Integer status2,String userName, PageSupport ps) {
        List<AqbProject> getProList=new ArrayList<>();

        try{
            ps.setTotalCount(aqbProjectMapper.selectCountOfProjects(createrId,proType,userName,status1,null));
            getProList=aqbProjectMapper.selectAllProject(createrId,proType,status1,null,userName,(ps.getCurrentPageNo()-1)*ps.getPageSize(),ps.getPageSize());

            if (null==getProList){
                System.out.println("AqbProjectServiceImpl.proList为空");
            }else {
                System.out.println("AqbProjectServiceImpl.proList成功");
            }
        } catch (Exception e) {
            System.out.println("AqbProjectServiceImpl.proList异常");
        }
        return getProList;
    }

    //根据项目编码进入详细页面
    @Override
    public AqbProject aqbPro(String proCode) {
        AqbProject aqbProject = null;
        try {
            aqbProject = aqbProjectMapper.detailedProInfo(proCode);
            if (null == aqbProject) {
                System.out.println("AqbProjectServiceImpl为空");
            } else {
                System.out.println("AqbProjectServiceImpl成功");
            }
        } catch (Exception e) {
            System.out.println("AqbProjectServiceImpl异常");
        }
        return aqbProject;
    }

    @Override
    public boolean updSatus(int proStatus,String proCode) {
        boolean flag =false;
        try {
            int i = aqbProjectMapper.proCheck(proStatus,proCode);
            if (i==0){
                LOGGER.debug("AqbProjectServiceImpl.updSatus 修改失败");
            }else{
                LOGGER.debug("AqbProjectServiceImpl.updSatus 修改成功");
                flag =true;
            }
        } catch (Exception e) {
            LOGGER.error("AqbProjectServiceImpl.updSatus"+e.getMessage());
        }
        return flag;
    }
}
